Skip to content

openai-agents-v2: use genai-utils for env var constant, histogram factories, and meter_provider#4456

Open
nagkumar91 wants to merge 4 commits intoopen-telemetry:mainfrom
nagkumar91:use-genai-utils-openai-agents-v2
Open

openai-agents-v2: use genai-utils for env var constant, histogram factories, and meter_provider#4456
nagkumar91 wants to merge 4 commits intoopen-telemetry:mainfrom
nagkumar91:use-genai-utils-openai-agents-v2

Conversation

@nagkumar91
Copy link
Copy Markdown
Contributor

Summary

Three improvements to align opentelemetry-instrumentation-openai-agents-v2 with opentelemetry-util-genai:

Changes

1. Env var constant — single source of truth

Remove _CONTENT_CAPTURE_ENV = "OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT" from __init__.py and import the constant from opentelemetry.util.genai.environment_variables.

2. Fix silently-ignored meter_provider

GenAISemanticProcessor._init_metrics() previously called get_meter(...) without any provider, so a caller-supplied meter_provider was silently ignored. This PR:

  • Adds meter_provider: Optional[MeterProvider] = None to GenAISemanticProcessor.__init__
  • Threads it through to _init_metrics(meter_provider)
  • Passes it to get_meter() along with the package's actual __version__ (was hardcoded as "0.1.0")

3. Semconv bucket boundaries via genai-utils histogram factories

Replace inline meter.create_histogram() calls with create_duration_histogram(meter) and create_token_histogram(meter) from opentelemetry.util.genai.instruments. This adds the semconv-specified explicit bucket boundaries that were previously missing from the agent's metrics.

Testing

All 103 existing tests pass.

…nd meter_provider

Three improvements to align with opentelemetry-util-genai:

1. Import OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT from
   opentelemetry.util.genai.environment_variables instead of re-defining
   it locally in __init__.py.

2. Add meter_provider parameter to GenAISemanticProcessor and thread it
   through _init_metrics(), fixing the bug where a caller-supplied
   MeterProvider was silently ignored.

3. Replace inline create_histogram() calls with create_duration_histogram()
   and create_token_histogram() from opentelemetry.util.genai.instruments,
   which adds the semconv-specified explicit bucket boundaries that were
   previously missing.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
nagkumar91 and others added 3 commits April 16, 2026 15:13
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Aligns opentelemetry-instrumentation-openai-agents-v2 with opentelemetry-util-genai by reusing shared env var/constants and instrument factories, and by correctly honoring a caller-provided MeterProvider when creating metrics instruments.

Changes:

  • Import OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT from opentelemetry.util.genai.environment_variables (single source of truth).
  • Thread an optional meter_provider into GenAISemanticProcessor metrics initialization and pass the package __version__ to get_meter().
  • Replace inline histogram creation with create_duration_histogram / create_token_histogram to apply semconv bucket boundaries.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
instrumentation-genai/opentelemetry-instrumentation-openai-agents-v2/src/opentelemetry/instrumentation/openai_agents/span_processor.py Adds meter_provider support for get_meter, uses package version, and switches to genai-utils histogram factories.
instrumentation-genai/opentelemetry-instrumentation-openai-agents-v2/src/opentelemetry/instrumentation/openai_agents/init.py Uses shared env var constant and passes meter_provider through instrumentor kwargs to the span processor.
instrumentation-genai/opentelemetry-instrumentation-openai-agents-v2/CHANGELOG.md Documents the alignment changes under Unreleased.

Comment on lines 541 to 545
self._meter = get_meter(
"opentelemetry.instrumentation.openai_agents", "0.1.0"
)

# Operation duration histogram
self._duration_histogram = self._meter.create_histogram(
name="gen_ai.client.operation.duration",
description="GenAI operation duration",
unit="s",
)

# Token usage histogram
self._token_usage_histogram = self._meter.create_histogram(
name="gen_ai.client.token.usage",
description="Number of input and output tokens used",
unit="{token}",
"opentelemetry.instrumentation.openai_agents",
__version__,
meter_provider,
)
Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new meter_provider plumbing fixes an important behavior change (caller-supplied providers were previously ignored), but it doesn’t look like there’s a unit test asserting the passed meter_provider is actually used by get_meter(...). Adding a small test with a stub MeterProvider (tracking get_meter calls) would prevent regressions.

Copilot uses AI. Check for mistakes.
Comment on lines 524 to +526
self._meter = None
self._duration_histogram: Optional[Histogram] = None
self._token_usage_histogram: Optional[Histogram] = None
self._duration_histogram: Optional[object] = None
self._token_usage_histogram: Optional[object] = None
Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_duration_histogram and _token_usage_histogram were widened from Optional[Histogram] to Optional[object], which loses type safety even though create_duration_histogram/create_token_histogram return Histogram. Consider importing Histogram and keeping these attributes typed as Optional[Histogram] (or a more specific protocol) so static checkers can validate .record() usage.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Approved PRs

Development

Successfully merging this pull request may close these issues.

3 participants